<?php

	include "kernel.php";
    include "config2.php";
	include "adw_functions.php";

    $link = ConnectToBase();

    $r = mysql_query("
    	update
        	squadrons
        set
   			addfuel = 0,
   			addammo = 0,
   			addrepair = 0,
            dfuel=0,
            dammo=0,
            drepair=0
            ");



	$TPmain->Necho = 0;
	$bd = mktime();
	$d = $bd;

	$allpilots_r = mysql_query("select * from finalpilots order by id");

    while ($pilot = mysql_fetch_assoc($allpilots_r))
    {

    	$i++;

        $squadronid =$pilot[squadronid];
    	$pilot = $pilot[pilotid];

        $sorties = mysql_query("select
        		 								id,
	                                            addfuel,
	                                            addammo,
	                                            addrepair,
	                                            dfuel,
	                                            dammo,
	                                            drepair

	               				from
                                	sorties
                                where
                                	pilotid='$pilot' order by id DESC");

         while ($sortie = mysql_fetch_assoc($sorties)) {


                mysql_query("update squadrons set addammo=addammo+'".$sortie[addammo]."', addfuel=addfuel+'".$sortie[addfuel]."', addrepair=addrepair+'".$sortie[addrepair]."', dfuel=dfuel+".$sortie[dfuel].",dammo=dammo+".$sortie[dammo].",drepair=drepair+".$sortie[drepair]." where id='".$squadronid."'");
	            mysql_query("update finalpilots set addammo=addammo+'".$sortie[addammo]."', addfuel=addfuel+'".$sortie[addfuel]."', addrepair=addrepair+'".$sortie[addrepair]."', dfuel=dfuel+".$sortie[dfuel].",dammo=dammo+".$sortie[dammo].",drepair=drepair+".$sortie[drepair]." where pilotid='".$pilot."'");

        }

		$d = mktime() - $d;
    	echo "$i. Done in ".$d."\r\n";
        $d = mktime();

        echo "\r\n";
        ob_end_flush();

    }


    $bd = mktime() - $bd;
    echo "Script done. \r\n Pilots parsed: $i \r\n In $bd seconds\r\n";

?>